export default{
  namespaced: true,
  state:{
    type:'all',
    list: JSON.parse(localStorage.getItem('vuex-todos-hf')) || [],
  },
  mutations:{
    changeDone(state,id){
    const obj=  state.list.find(item=>item.id===id)
    obj.isDone=!obj.isDone
    },
    delById(state,id){
      state.list=state.list.filter(item=>item.id!==id)
    },
    add(state,content){
      state.list.unshift({
        id: +new Date(),
        name: content,
        isDone: false
      })
    },
    changeIsAll(state,val){
      state.list.forEach(item=>item.isDone=val)
    },
    clearDone(state){
      state.list=state.list.filter(item=>!item.isDone)
    },
    changeType(state,val){
      state.type=val
    }
  },
  getters:{
    leftCount(state){
      return state.list.filter(item=>!item.isDone).length
    },
    showClear(state){
      return state.list.some(item=>item.isDone)
    },
    showList(state){
      if (state.type==='active') {
        return state.list.filter(item=>!item.isDone)
      }else if (state.type==='complete') {
        return state.list.filter(item=>item.isDone)
      }else{
        return state.list
      }
    }
  }
}